package com.ycl.plus.lm.hardware.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.tree.Tree;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ycl.plus.lm.common.core.utils.MapstructUtils;
import com.ycl.plus.lm.common.core.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ycl.plus.lm.common.core.utils.TreeBuildUtils;
import com.ycl.plus.lm.hardware.domain.HardwareEquipSensor;
import com.ycl.plus.lm.hardware.mapper.HardwareEquipSensorMapper;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import com.ycl.plus.lm.hardware.domain.bo.HardwareSensorBo;
import com.ycl.plus.lm.hardware.domain.vo.HardwareSensorVo;
import com.ycl.plus.lm.hardware.domain.HardwareSensor;
import com.ycl.plus.lm.hardware.mapper.HardwareSensorMapper;
import com.ycl.plus.lm.hardware.service.IHardwareSensorService;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.stream.Collectors;

/**
 * 传感器Service业务层处理
 *
 * @author devil-ycl
 * @date 2024-11-07
 */
@RequiredArgsConstructor
@Service
public class HardwareSensorServiceImpl implements IHardwareSensorService {

    @SuppressWarnings("all")
    private final HardwareSensorMapper baseMapper;
    /**
     * 查询传感器
     *
     * @param id 主键
     * @return 传感器
     */
    @Override
    public HardwareSensorVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }


    /**
     * 查询符合条件的传感器列表
     *
     * @param bo 查询条件
     * @return 传感器列表
     */
    @Override
    public List<HardwareSensorVo> queryList(HardwareSensorBo bo) {
        LambdaQueryWrapper<HardwareSensor> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HardwareSensor> buildQueryWrapper(HardwareSensorBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HardwareSensor> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), HardwareSensor::getName, bo.getName());
        return lqw;
    }

    /**
     * 新增传感器
     *
     * @param bo 传感器
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(HardwareSensorBo bo) {
        HardwareSensor add = MapstructUtils.convert(bo, HardwareSensor.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改传感器
     *
     * @param bo 传感器
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(HardwareSensorBo bo) {
        HardwareSensor update = MapstructUtils.convert(bo, HardwareSensor.class);
        assert update != null;
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(HardwareSensor entity) {
        //TODO 做一些数据校验,如唯一约束
        LocalDateTime now = LocalDateTime.now();
        if (entity.getId() != null) {
            entity.setUpdateTime(now);
        } else {
            entity.setCreateTime(now);
        }
    }



    /**
     * 校验并批量删除传感器信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

    @Override
    public int updateSensorStatus(Long id, String status) {
        return baseMapper.update(null,
            new LambdaUpdateWrapper<HardwareSensor>()
                .set(HardwareSensor::getStatus, status)
                .eq(HardwareSensor::getId, id));
    }


    @Override
    public List<HardwareSensorVo> selectSensorList(HardwareSensorBo sensor) {
        LambdaQueryWrapper<HardwareSensor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(HardwareSensor::getStatus, "0");
        return baseMapper.selectVoList(wrapper);
    }

    @Override
    public List<Tree<Long>> buildSensorTreeSelect(List<HardwareSensorVo> list) {
        if (CollUtil.isEmpty(list))
            return CollUtil.newArrayList();
        return TreeBuildUtils.build(list, (v, tree) ->
            tree.setId(v.getId())
                .setParentId(v.getParentId())
                .setName(v.getName())
                .setWeight(1)
        );
    }
}
